from django.shortcuts import render
from django.http import HttpResponse,HttpResponseRedirect
import requests
from .setting import *
from tower.models import *
from asset.models import *
from login.models import *

# Create your views here.
#首页
def homepage(request):
    return render(request,'apitower/homepage.html')
#增加楼房
def add_building(request):
    """
            @author:薛威宁
            @date:2018/10/16/10:40

            提交方式 ：POST
            提交数据格式 ：{'add_tower_name':'xxx'}
        """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/add_building/'
        r = requests.post(url_ip,data = info)
        print(r.text)
        if '楼房添加成功' == r.json()['message']:
            return render(request,'apitower/成功.html')
        else:
            return HttpResponse(r.json()['message'])
    elif request.method == 'GET':
        return render(request, 'apitower/add_building.html')

#添加楼层
def add_floor(request):
    """
            @author:薛威宁
            @date:2018/10/16/10:40

            提交方式 ：POST
            提交数据格式 ：{'add_tower_name':'xxx'}
        """
    if request.method == 'POST':
        info = dict(request.POST)
        #print(info)
        url_ip = url + 'tower/add_floor/'
        r = requests.post(url_ip,data = info)
        #print(r.text)
        if '楼层添加成功' == r.json()['message']:
            return render(request,'apitower/成功.html')
        else:
            return HttpResponse(r.json()['message'])
    elif request.method == 'GET':
        return render(request, 'apitower/floor.html')
#添加房间
def add_room(request):
    """
                @author:薛威宁
                @date:2018/10/16/10:40

                提交方式 ：POST
                提交数据格式 ：{'add_room_num':'xxx'}
            """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/add_room/'
        r = requests.post(url_ip,data = info)
        print(r.text)
        if '房间添加成功' == r.json()['message']:
            return render(request,'apitower/成功.html')
        else:
            return HttpResponse(r.json()['message'])
    elif request.method == 'GET':
        return render(request, 'apitower/room.html')


def api_tower(request):
    """

    删除楼房
       @author:郭峰
        @date:2018/10/16

        提交方式 ：POST
        提交数据格式 ：{'add_tower_name':info[add_tower_name'][0]}
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/del_tower/'
        r = requests.post(url_ip,data={'add_tower_name':info['add_tower_name'][0]})
        print(r.text)
        if '删除成功' ==r.json()['message']:
            context = {'message':r.json()['message']}
            return render(request,'apitower/del_api.html',context)
        else:
            context = {'message':r.json()['message']}
            return render(request, 'apitower/del_api.html', context)

    elif request.method == 'GET':
        return render(request, 'apitower/del_api.html')

def api_floor(request):
    """
    删除楼层
        @author:郭峰
        @date:2018/10/16

        提交方式 ：POST
        提交数据格式 ：{'add_tower_name':'xxx','floor_num':info['add_floor_num'][0]}
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/del_floor/'
        r = requests.post(url_ip,data=info)
        print(r.text)
        if '删除楼层成功' == r.json()['message']:
            context = {'message':r.json()['message']}
            return render(request,'apitower/def_floor.html',context)
        else:
            context = {'message': r.json()['message']}
            return render(request,'apitower/def_floor.html', context)
    elif request.method == 'GET':
        return render(request, 'apitower/def_floor.html')
def api_room(request):
    """
    删除房间
         @author:郭峰
        @date:2018/10/16

        提交方式 ：POST
    提交数据格式 ：{'add_tower_name':'xxx',
    'floor_num':info['add_floor_num'][0],
    'roon_num':info['add_room_num'][0]}
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/del_room/'
        r = requests.post(url_ip,data=info)
        if '删除房间成功' == r.json()['message']:
            context = {'message':r.json()['message']}
            return render(request,'apitower/del_room.html',context)
        else:
            context = {'message': r.json()['message']}
            return render(request, 'apitower/del_room.html', context)
    elif request.method == 'GET':
        return render(request, 'apitower/del_room.html')

def alter_tower(request):
    """
    更爱楼房
    @auther: 郭峰
    @date: 2018/10/16
    提交方式: POST
    提交数据格式: #####这地方看下数据库!!!!!!!!
                {'tower_name': ['宿舍楼'], 'new_tower_name': ['黄鹤楼']}
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/change_tower/'
        r = requests.post(url_ip, data=info)
        print(r.text)
        if '楼名修改成功' == r.json()['message']:
            context = {'message':r.json()['message']}
            return render(request,'apitower/chanage_tower.html',context)
        else:
            context = {'message':r.json()['message']}
            return render(request,'apitower/chanage_tower.html',context)
    elif request.method == 'GET':
        return render(request, 'apitower/chanage_tower.html')
def alter_floor(request):
    """
    更改楼层名
        @auther: 郭峰
        @date: 2018/10/16
        提交方式: POST
        提交数据格式: {   #####这地方看下数据库!!!!!!!!
                      'tower_name': ['烂尾楼'],
                      'floor_num': ['5'],
                      'new_tower_name': ['黄鹤楼'],
                      'new_floor_num': ['8']}
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'change_floor/'
        r = requests.post(url_ip, data=info)
        print(r.text)
        if '楼层信息修改成功' == r.json()['message']:
            context = {'message':r.json()['message']}
            return render(request,'apitower/chanage_floor.html',context)
        else:
            context = {'message':r.json()['message']}
            return render(request,'apitower/chanage_floor.html',context)
    elif request.method == 'GET':
        return render(request, 'apitower/chanage_floor.html')
def alter_room(request):
    """
    更改房间
        @auther: 郭峰
        @date: 2018/10/16
        提交方式: POST
        提交数据格式: {  #####这地方看下数据库!!!!!!!!
                       'tower_name': ['观音楼'],
                       'floor_num': ['8'],
                       'room_num': ['888']
                       'new_tower_name': ['11'],
                       'new_floor_num': ['22'],
                       'new_room_num': ['33'],
                       }
    """
    if request.method == 'POST':
        info = dict(request.POST)
        print(info)
        url_ip = url + 'tower/change_room/'
        r = requests.post(url_ip, data=info)
        print(r.text)
        if '房间信息修改成功' == r.json()['message']:
            context = {'message':r.json()['message']}
            return render(request,'apitower/chanage_room.html',context)
        else:
            context = {'message':r.json()['message']}
            return render(request,'apitower/chanage_room.html',context)
    elif request.method == 'GET':
        return render(request, 'apitower/chanage_room.html')
#展示楼房,楼层,房间
def show_buildings(request):
    """
       查看楼,楼房,房间的展示
           @auther: 薛威宁
           @date: 2018/10/25
           展示的格式: {  序号:{{forloop.counter}}
                          楼层:{{x.floor.tower.tower_name}}
                          楼房:{{x.floor.floor_num}}
                          房间:{{x.room_num}}
                          }
       """
    show = Room.objects.all()
    context = {'a':show}
    return render(request,'apitower/show_buildings.html',context)









